package svgCanvas;

import eventHandling.fileHandling.FileLoadHandler;
import eventHandling.fileHandling.FileSaveHandler;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.io.File;
import javax.swing.*;
import org.apache.batik.bridge.UpdateManager;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.swing.gvt.GVTTreeRendererAdapter;
import org.apache.batik.swing.gvt.GVTTreeRendererEvent;
import org.apache.batik.swing.svg.GVTTreeBuilderAdapter;
import org.apache.batik.swing.svg.GVTTreeBuilderEvent;
import org.apache.batik.swing.svg.SVGDocumentLoaderAdapter;
import org.apache.batik.swing.svg.SVGDocumentLoaderEvent;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;


/**
 * This class encapsulates SVG canvas and operations using it
 * @author Martin
 */
public class SVGWindow {
    /**
     * The application main window
     */
    protected JFrame frame;
    /**
     * Load button
     */
    protected JButton button = new JButton("Load");
    /**
     * Save button
     */
    protected JButton saveButton = new JButton("Save");
    /**
     * Undo button
     */
    protected JButton undoButton = new JButton("Undo");
    /**
     * Label showing messages when loading, building and printing svg document
     */
    protected JLabel label = new JLabel();
    /**
     * SVG canvas used for displaying / changing svg files
     */
    public JSVGCanvas svgCanvas = new JSVGCanvas();
    /**
     * 
     */
    public File svgDocumentFile;
    /**
     * color chooser - user can choose colours of his anotations
     */
    public JColorChooser colorChooser;
    /**
     * text field to input anotation text
     */
    public JTextField annotationTextField = new JTextField("Enter annotation text", 30); 
    
    public JComboBox shapeChooser = new JComboBox();
    
    public JCheckBox includeText = new JCheckBox();


    JPanel panel = new JPanel(new BorderLayout());
    JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));

    /**
     * 
     * @param f main application window
     */
    public SVGWindow(JFrame f) {
        frame = f;
    }

    /**
     * Method creates the panel, initializes buttons and listeners
     * for svg canvas load events
     * @return returns component with all created objects included (buttons, labels, colorchooser,..) and with listeners for them set
     */
    public JComponent createComponents() {    
        p.add(button);
        p.add(saveButton);
        p.add(undoButton);
        p.add(label);
        
        JPanel colorChooserPanel = new JPanel(new BorderLayout());
        JPanel upperUpperColorChooserPanel = new JPanel(new BorderLayout());
        JPanel upperColorChooserPanel = new JPanel(new BorderLayout());
        JPanel includeTextPanel = new JPanel(new BorderLayout());
        JPanel shapeChooserPanel = new JPanel(new BorderLayout());
        
        shapeChooser.addItem("Rectangle");
        shapeChooser.addItem("Ellipse");
        
        colorChooser = new JColorChooser();
        colorChooser.setBorder(BorderFactory.createTitledBorder("Choose Text Color"));

        includeTextPanel.add("West", new JLabel("Include text"));
        includeTextPanel.add("East", includeText);
        shapeChooserPanel.add("West", new JLabel("Choose shape"));
        shapeChooserPanel.add("East", shapeChooser);
        
        upperColorChooserPanel.add("North", shapeChooserPanel);
        upperColorChooserPanel.add("South", includeTextPanel);
        
        upperUpperColorChooserPanel.add("North", annotationTextField);
        annotationTextField.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(java.awt.event.FocusEvent evt) {
                if(annotationTextField.getText().equals("Enter annotation text"))
                {
                    annotationTextField.setText("");
                }
            }

            @Override
            public void focusLost(FocusEvent e) {
                if(annotationTextField.getText().equals(""))
                {
                    annotationTextField.setText("Enter annotation text");
                }
            }
        });
        upperUpperColorChooserPanel.add("South", upperColorChooserPanel);
        
        colorChooserPanel.add("Center", colorChooser);
        colorChooserPanel.add("North", upperUpperColorChooserPanel);
        
        panel.add("East", colorChooserPanel);
        panel.add("North", p);
        panel.add("Center", svgCanvas);

        InitializeListeners();
        return panel;
    }
    
    private void InitializeListeners()
    {
        // Set the button action.
        button.addActionListener(new FileLoadHandler(svgCanvas, panel, frame)); 
        saveButton.addActionListener(new FileSaveHandler(svgCanvas, panel));
        undoButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                UpdateManager updateManager = svgCanvas.getUpdateManager();
                
                updateManager.getUpdateRunnableQueue().invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        Document a = svgCanvas.getSVGDocument();
                    Element root = a.getDocumentElement();
                    
                    Node e = root.getLastChild();
                    root.removeChild(e);
                    }
                });
                
            }
        });

        // Set the JSVGCanvas listeners - messages when loading, building and rendering SVG document
        svgCanvas.addSVGDocumentLoaderListener(new SVGDocumentLoaderAdapter() {
            @Override
            public void documentLoadingStarted(SVGDocumentLoaderEvent e) {
                label.setText("Document Loading...");
            }
            @Override
            public void documentLoadingCompleted(SVGDocumentLoaderEvent e) {
                label.setText("Document Loaded.");
            }
        });

        svgCanvas.addGVTTreeBuilderListener(new GVTTreeBuilderAdapter() {
            @Override
            public void gvtBuildStarted(GVTTreeBuilderEvent e) {
                label.setText("Build Started...");
            }
            @Override
            public void gvtBuildCompleted(GVTTreeBuilderEvent e) {
                label.setText("Build Done.");
                frame.pack();
            }
        });

        svgCanvas.addGVTTreeRendererListener(new GVTTreeRendererAdapter() {
            @Override
            public void gvtRenderingPrepare(GVTTreeRendererEvent e) {
                label.setText("Rendering Started...");
            }
            @Override
            public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
                label.setText("");
                frame.pack();
            }
        });
    }
}

